package com.company;

import java.util.ArrayList;

public class Task5 {
    public static void main(String[] args) {
        //todo 4.写一段代码，实现System.arraycopy方法的功能。
        String[] src = {"aaa", "bbb", "ccc", "ddd", "eee", "fff"};
        String[] dest = {"ooo", "ppp", "qqq", "rrr", "sss", "ttt", "uuu", "vvv", "www"};
        String[] rt = arraycopy(src, 2, dest, 5, 4);
        for (int i = 0; i < rt.length; i++) {
            System.out.println(rt[i]);
        }
    }

    public static String[] arraycopy(String[] src, int srcPos, String[] dest, int destPos, int length) {
        int end = srcPos + length; //计算原素组需要复制的最大下标
        int lt = dest.length + src.length;
        String[] str = new String[lt];
        for (int i = 0; i < destPos; i++) {
            str[i] = dest[i];
        }
        for (int i = srcPos; i < end; i++) {
            str[i] = src[i];
        }
        if (dest.length >= (srcPos + length)) {
            for (int i = end; i < dest.length; i++) {
                str[i] = dest[i];
            }
        }

        int maxI = 0;
        for (int i = 0; i < str.length; i++) {
           if(str[i] == null){
               maxI = i;
               break;
           }
        }
        String[] newStr = new String[maxI];
        for (int i = 0; i < str.length; i++) {
            if(str[i] != null){
                newStr[i] = str[i];
            }
        }
        return newStr;
    }

}
